home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1993 / MacHack 1993.toast / MacHack™ 1987-1992 / MacHack™ '90 / Utilities ƒ / MPW Tools ƒ / Simula4.07 / Simula 4.07ƒ / SExamples / sample.sim < prev   
Encoding:
Text File  |  1989-05-02  |  9.6 KB  |  285 lines  |  [TEXT/MPS ]

  1. ! FILE: Sample.sim
  2. ! This little program is written to demonstrate the Simula interface to the
  3. ! Macintosh Toolbox. It displays a single, fixed size window in which the user
  4. ! can enter and edit text. It is written to correspond to similar examples
  5. ! written in pascal and other languages.
  6. !
  7. ! Many routine tasks are automatically taken care of in the SIMULA toolbox
  8. ! interface:
  9. ! - Standard events are filtered out (for menu selection, system events etc) and
  10. !   the events targeted for application Windows are dispatched to the appropriate
  11. !   Window process. The Window is selected and the coordinates are converted to 
  12. !   the local coordinates of the Window.
  13. ! - Menu selection is done automatically so the user program only has to register
  14. !   a menu item to take care of the application specific actions when selected
  15. !   by the user. There is also a ready made menu for the leftmost Apple menu
  16. !   which runs desk accessoirs etc.
  17. ! This file is "compiled" with the command:
  18. !
  19. !       Rez sample.r -o sample -append
  20. !       Setfile -a B sample -c LUND -t APPL 
  21. !        simcomp sample
  22. ! the program should be linked with:
  23. !     simld sample -toolbox -APPL
  24. ! which means that it can be executed as an application.
  25. ! This sequence can be generated by entering "make sample"
  26. ! ---------------------------------------------------------------------------  ;
  27. begin
  28.  
  29.     external class MacEvent="::SInterfaces:MacEvent";
  30.     external class MacProcessMgr="::SInterfaces:MacProcessMgr";
  31.     external class MacPoint="::SInterfaces:MacPoint";
  32.     external class MacRect="::SInterfaces:MacRect";
  33.     external class MacWindow="::SInterfaces:MacWindow";
  34.     external class MacTextEdit="::SInterfaces:MacTextEdit";
  35.  
  36.     external class DefaultMenuProcess="::SInterfaces:DefaultMenuProcess";
  37.     external class Macmenubar="::SInterfaces:MacMenuBar";
  38.     external class MacMenu="::SInterfaces:MacMenu";
  39.     external class Macmenuitem="::SInterfaces:MacMenuitem";
  40.     external class DefaultAppleMenu="::SInterfaces:DefaultAppleMenu";        
  41.     
  42.     external class MacDialogMgr="::SInterfaces:MacdialogMgr";
  43.     external class MacDialog="::SInterfaces:Macdialog";    
  44.     external class MacCursor="::SInterfaces:MacCursor";
  45.  
  46. !---------------------------------------------------
  47. ! Global data structures and singular system objects
  48. !---------------------------------------------------;
  49.  
  50.     ref(MacProcessMgr) theMGR;  ! This class takes care of events and  ;
  51.                                 ! process scheduling - always make ONE ;
  52.     ref(MacMenuBar) theMenuBar; ! -- Create also one MenuBar and one -- ;
  53.     ref(defaultMenuProcess) theMenuBarProcess; ! -- Process to handle it -- ;
  54.         
  55.     ! In this exsample there is only one Window and associated Process ;
  56.     ref(TextWindow) myTextWindow;
  57.     ref(TextWindowProcess) myTextWindowProcess;
  58.     
  59.     ! ID:s of Menu descriptions in the resource file. ;
  60.     short integer fileID=129, editID=130;
  61.     ref(DefaultAppleMenu) theAppleMenu;
  62.     ref(MacMenu) theFileMenu, theEditMenu;
  63.  
  64. !---------------------------------------------------
  65. ! Window and Process definition
  66. !---------------------------------------------------;
  67.  
  68.     ! TextWindow: in this example all window output is controlled
  69.     ! by the TE-package, so the TextWindow is almost Empty 
  70.     !-------------------------;
  71.     MacWindow class TextWindow;
  72.     begin
  73.         ref(MacTextEdit) myTE;
  74.         short integer windowId=128;  ! The description of this Window is stored ;
  75.         GetNewWindow(windowId, none);! in the resourcefile: sample.r, ID: 128 ;
  76.     end;
  77.     
  78.     ! TextWindowProcess: Waits for events targeted for the Window and 
  79.     ! does the neccessary actions as they arrive.
  80.     !--------------------------------- ;
  81.     MacProcess class TextWindowProcess(MyW); ref(TextWindow) MyW;
  82.     begin
  83.         ref(MacCursor) iBeamCursor, arrowCursor;
  84.         ref(MacRect) TXRect,WPort;    
  85.         ref(MacEvent) theEvent;
  86.         ref(MacPoint) ClickPoint;
  87.  
  88.         boolean procedure PtInRect(aPoint, aRect);
  89.             ref(MacPoint) aPoint; ref(MacRect) aRect;
  90.         begin
  91.             inspect aPoint do
  92.             inspect aRect do
  93.                 PtInRect:=    Top<V and then V<Bottom and then
  94.                                 Left<H and then H<Right;
  95.         end;
  96.         ! -- Create Objects and initialize data structures
  97.         ! -- local to this process -- ;
  98.         
  99.         TXRect:-new MacRect;
  100.         MyW.GetPortRect(TXRect);  ! Get the Bounds of the Window ;
  101.         MyW.myTE:-new MacTextEdit;
  102.         MyW.myTE.TEnew(TXRect,TXRect);
  103.  
  104.         theEvent:-new MacEvent;
  105.         ClickPoint:-new MacPoint;
  106.         WPort:-new MacRect;
  107.         MyW.GetPortRect(WPort);
  108.         
  109.         iBeamCursor:-new MacCursor;
  110.         iBeamCursor.GetCursor(TConst.iBeamCursor);
  111.         arrowCursor:-new MacCursor;
  112.  
  113.         ! -- Enable the Events we are prepared to take care of.
  114.         ! -- Other events for this process are discarded. --;
  115.         EnableEvent(TConst.keyDown);
  116.         EnableEvent(TConst.mouseDown);
  117.         EnableEvent(TConst.updateEvt);
  118.         EnableEvent(TConst.activateEvt);
  119.         EnableEvent(TConst.NullEvent);
  120.  
  121.         myW.myTE.TEActivate; ! Our window is active to start with ;
  122.         ! -- Main event loop of this process -- ;
  123.         while true do
  124.         begin
  125.             ! -- Set cursor to match being inside/outside the Window --;
  126.             if MyW == theMGR.FrontWindow then
  127.             begin
  128.                 theMGR.EventMgr.GetMouse(ClickPoint);
  129.                 if PtInRect(ClickPoint, WPort) then
  130.                       iBeamCursor.SetCursor
  131.                 else
  132.                     arrowCursor.InitCursor;
  133.             end;
  134.             ! -- This process Halts here until an Event is available,
  135.             ! -- but since we have enables also NullEvents we will get 
  136.             ! -- these repeatedly when our window is active.  --;
  137.             WaitNextEvent(theEvent);
  138.             ! -- Analyze What kind of Event we gott -- ;
  139.             if theEvent.what=tconst.NullEvent then
  140.                 myW.myTE.TEIdle  !call TextEdit to make vertical bar blink;
  141.             else if theEvent.what=TConst.keyDown then
  142.                 myW.myTE.TEKey(theEvent.keyPressed)
  143.             else if theEvent.what=TConst.mouseDown then
  144.             begin
  145.                 theEvent.CopyPoint(Clickpoint);
  146.                 myW.myTE.TEClick(ClickPoint,theEvent.Shiftkey);
  147.             end
  148.             else if theEvent.what=TConst.updateEvt then
  149.             begin
  150.                 myW.BeginUpdate;
  151.                 myW.EraseRect(TXRect);
  152.                 myW.myTE.TEUpdate(TXRect);
  153.                 myW.EndUpdate;
  154.             end
  155.             else if theEvent.what=TConst.activateEvt then
  156.             begin
  157.                 if theEvent.activated then
  158.                     myW.myTE.TEActivate
  159.                 else
  160.                     myW.myTE.TEDeactivate;            
  161.             end;
  162.         end;
  163.     end --- Text Window process  ---;
  164.  
  165. ! ---------------------------------------------------
  166. ! Menu related classes and procedures
  167. ! ---------------------------------------------------;
  168.     
  169.     ! InitMenus: This procedure initializes the menus and creates
  170.     ! the neccessary MenuItem sub-class objects to react to
  171.     ! menu selection. In this case there are three pull-down menus.
  172.     !------------------;
  173.     procedure InitMenus;
  174.     begin    
  175.         ! -- Apple-menu: use the default version but register
  176.         ! -- an About-item of our own. ;
  177.         theAppleMenu:-new DefaultAppleMenu;    
  178.         theMenuBar.InsertMenu(theAppleMenu,0);
  179.         theAppleMenu.RegisterItem(new AboutSimItem,1);
  180.         ! -- the File menu we are building directly --;
  181.         theFileMenu:-new MacMenu;    
  182.         theFileMenu.NewMenu(FileId,"File");
  183.         theMenuBar.InsertMenu(theFileMenu,0);
  184.         theFileMenu.AppendMenu("Quit");
  185.         theFileMenu.RegisterItem(new QuitItem,1);
  186.         ! -- The Edit menu is described in the resource file 
  187.         ! -- so it is simply read in. For each item there
  188.         ! -- is created an object to do the actions. They
  189.         ! -- all have a procedure "doMenu" that is called
  190.         ! -- when the alternative is selected. -- ;
  191.         theEditMenu:-new MacMenu;
  192.         theEditMenu.getMenu(EditId);
  193.         theMenuBar.InsertMenu(theEditMenu,0);
  194.         theEditMenu.RegisterItem(new CutItem,3);
  195.         theEditMenu.RegisterItem(new CopyItem,4);
  196.         theEditMenu.RegisterItem(new PasteItem,5);
  197.         theEditMenu.RegisterItem(new ClearItem,6);        
  198.         ! -- Draw the updated menu Bar to make it visible -- ;
  199.         theMenuBar.DrawMenuBar;
  200.     end -- Init menus -- ;
  201.     
  202.     ! MacMenuItem: One subclass for each menu alternative.
  203.     !-------------------------;
  204.     MacMenuItem class QuitItem;
  205.     begin
  206.         procedure doMenu(m,i); integer m,i;
  207.             theMGR.Stop;
  208.     end;
  209.     
  210.     MacMenuItem class CutItem;
  211.     begin
  212.         procedure doMenu(m,i); integer m,i;
  213.             myTextWindow.myTE.TECut;
  214.     end;
  215.     
  216.     MacMenuItem class CopyItem;
  217.     begin
  218.         procedure doMenu(m,i); integer m,i;
  219.             myTextWindow.myTE.TECopy;
  220.     end;
  221.     
  222.     MacMenuItem class PasteItem;
  223.     begin
  224.         procedure doMenu(m,i); integer m,i;
  225.             myTextWindow.myTE.TEPaste;
  226.     end;
  227.     
  228.     MacMenuItem class ClearItem;
  229.     begin
  230.         procedure doMenu(m,i); integer m,i;
  231.             myTextWindow.myTE.TEDelete;
  232.     end;
  233.  
  234.     MacMenuItem class AboutSimItem;
  235.     begin
  236.  
  237.         procedure doMenu(m,i); integer m,i;
  238.         begin
  239.             ref(MacRect) itemRect;
  240.             ref(MacDialogMgr) myDialogMgr;
  241.             ref(MacDialog) myDialog;
  242.             short integer itemHit,itemType,
  243.                 aboutMeDLOG = 128, okButton = 1, authorItem = 2,languageItem = 3;
  244.             integer ItemHdl;
  245.  
  246.             itemRect:-new MacRect;
  247.     
  248.             myDialogMgr:- new MacDialogMgr(theMGR);
  249.             myDialog:-new MacDialog;
  250.             myDialog.GetNewDialog(aboutMeDLOG, none);
  251.     
  252.             myDialog.GetDitem(  authorItem, itemType, itemHdl, itemRect);
  253.             mydialog.GetDitem(languageItem, itemType, itemHdl, itemRect);
  254.     
  255.             itemhit:=0;
  256.             while itemHit <> okButton do
  257.                 myDialogMgr.ModalDialog(0, itemHit);
  258.                 
  259.             myDialog.disposdialog;
  260.             myTextWindow.myTE.TEActivate;
  261.         end;
  262.     end;
  263.     
  264. !-------------------------------------------------
  265. ! Initialize Global objects and data structures
  266. !-------------------------------------------------;
  267.     ! -- One of each of these -- ;
  268.     theMGR:-new MacProcessMgr;
  269.     theMenuBar:-new MacMenuBar;
  270.     theMenuBarProcess:- new DefaultMenuProcess(theMenuBar);
  271.     theMGR.RegisterMenuProcess(theMenuBarProcess);
  272.     InitMenus; ! -- Create menu alternatives -- ;
  273.     
  274.     ! -- Create our single window and make it known to the Manager -- ;
  275.     myTextWindow:-new TextWindow;
  276.     theMGR.RegisterWindow(myTextWindow);    
  277.     ! -- Create the process to control the window and make it known --;
  278.     myTextWindowProcess:-new TextWindowProcess(myTextWindow);    
  279.     theMGR.RegisterProcess(myTextWindowProcess, myTextWindow);
  280.  
  281.     ! -- Leave control to the manager --;
  282.     theMGR.Run;
  283.     ! -- code here will be executed when the manager has been "Stopped";
  284. end